//global vars
var vd_subscribed		= new Array();
var vd_downloaded 	= new Array();
var vd_favourite 		= new Array();
var yearlist		 		= new Array();
var vd_all					= new Array();
var userdetails 		= new Array();
var vd_latest				= new Array();
var subscribevideolist 	= new Array();
var flag_buy_videoid 		= 0;
var flag_buy_status 		= 0;
var flag_operation 			= '';
var flag_confirm_id 		= 0;
var details_begins			= "";
var details_ends				= "";
var div_favourite				= "";
var div_purchased				= "";
	
$(document).ready(function(){
	var cnt				= 2;
	var emptyData ="<div> Ooops! Video data is empty</div>";
	
	$.urlParam = function(name){
		var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
		return results[1] || 0;
	}
	var userid 	= $.urlParam('userid');
	$("#txt_userid").val(userid);
	getalldata(userid);
	var setting_flag = 0;
	var managevideo_flag = 0;
                  
    
	//Setting pop up
	$('#li_setting').click(function() {		
		if(setting_flag==0){               

			var img01 = "themes/apple/cpd/all_videos_btn.png";
			var img02 = "themes/apple/cpd/manage_videos_btn.png";
			var tempstr = '<div id="dv_setting" style="bottom: 0; opacity:1.0; display: block; z-index:500; background-color:transparent; margin-bottom: 46px; position: fixed;"><div style="background-color:transparent; font-size: 13px; font-weight:bold;"><div style="text-align:center; border:4px solid #000; background-color:#F5E5E8; border-radius:5px"><div style="text-align:left; padding-top:15px; padding-left:15px;" class="dv09">Show videos:</div><div style="background: url('+img01+') repeat-x scroll 0% 0% transparent; width:264px; height:24px; border-radius: 5px; color: #fff;  margin: 0px 14px 10px 14px;"><a href="#allvideos" style="color:#fff; text-decoration:none;"><span style="width: 85px; float:left; padding-top: 3px;">All Videos</span></a><a href="#dv_downloaded" style="color:#fff; text-decoration:none;"><span style="width: 92px; height:20px; border-right:1px solid #777; border-left:1px solid #777; float:left; padding-top: 3px;">Downloads</span></a><a href="#dv_favourites" style="color:#fff; text-decoration:none;"><span style="width: 85px; float:left; padding-top: 3px;">Favourites</span></a></div><div style="background: url('+img02+') repeat-x scroll 0% 0% transparent; width:264px; height:24px; border-radius: 5px; color: #fff; font-size: 13px; font-weight:bold; margin: 0px 14px 15px 14px;"><a href="javascript:showmanagevideopopup();" style="color:#fff; text-decoration:none;"><span style="width: 264px; float:left; padding-top:3px;">Manage Videos</span></a></div></div></div><div id="dv_anchor" style="float:right; line-height:4px; padding-right:9%;"><img src="themes/apple/cpd/settings_anchor.png"></div></div>';
							//$('#bg_settingbox').html(tempstr);		
			$("#jqt").append(tempstr);
			$('#bg_settingbox').fadeIn();
			$('#dv_setting').css("min-height", "143px");
							$('#dv_setting').css("height", "143px");  
			doOnOrientationChange();
							//$('#dv_setting').css("z-index", "9999");
			setting_flag = 1;
		} else if(setting_flag==1) {
				 closesettingbox(); //Close Setting pop up
		}
	});		
		
		
	//manage video pop up
	showmanagevideopopup = function() {
		if(managevideo_flag==0){
			managevideolist();
			$('#dv_managevideos').fadeIn();
			managevideo_flag = 1;
		} else if(managevideo_flag==1) {
			$('#dv_managevideos').fadeOut();
			managevideo_flag = 0;
		}
		//change height on orientation detection
		doOnOrientationChange();
	};
                  
	//close manage video pop up
	$('.donebutton').click(function() {			
		$('#dv_managevideos').fadeOut();
		managevideo_flag = 0;
	});

	//Close Setting pop up
	$('#bg_settingbox').click(function() {	
		closesettingbox(); //Close Setting pop up
	});

	//Close Setting pop up
	closesettingbox = function() {
		if(setting_flag==1) {
			$('#bg_settingbox').fadeOut(300);
			$('#dv_setting').fadeOut(300);
			$('#dv_setting').remove();
			setting_flag = 0;
		}
	};
	
	//select all videos from manage video
	$('.selectallbutton').click(function() {	
		$(".checkbox").each( function() {
			$(this).attr("checked",status);
		})
	});

	//delete all videos from manage video
	$('.deletebutton').click(function() {
		var download_vd_array  = new Array();
		var favourite_vd_array = new Array();
		var removeItem = 0;
		$("input:checkbox[name='chk_dwnload[]']:checked").each(function(){
			download_vd_array.push($(this).val());
		});
		$("input:checkbox[name='chk_favourite[]']:checked").each(function(){
			favourite_vd_array.push($(this).val());
		});
		if(favourite_vd_array.length > 0 || download_vd_array.length > 0){
			$.ajax({
				url: BASEURL+'home/deletefrom_managevideo',
				type: "POST",
				dataType: '',
				data: {dwn_list:download_vd_array, fav_list:favourite_vd_array, uid:userid},
				success: function(responce){
					showAlert("Delete Video","Selected videos deleted successfully.");
					if(download_vd_array.length > 0){
						for(var i=0; i< download_vd_array.length; i++){
							removeItem = download_vd_array[i];
							vd_downloaded = jQuery.grep(vd_downloaded, function(value) {
								return value != removeItem;
							});
						}
						$("#dv_dwnldvideolist").html('');
						showvideolist(vd_downloaded,"dv_dwnldvideolist");
					}
					if(favourite_vd_array.length > 0){
						for(var j=0; j< favourite_vd_array.length; j++){
							removeItem = favourite_vd_array[j];
							vd_favourite = jQuery.grep(vd_favourite, function(value) {
								return value != removeItem;
							});
						}
						$("#dv_favouritesvideolist").html('');
						showvideolist(vd_favourite,"dv_favouritesvideolist");
					}
					
					managevideolist();
				},
				error: function() {
					showAlert("System Error","There has been an Error with the Settings.");
				}
			});
		}else{
			showAlert("Delete Video","Please select atleast one video.");
		}
  });
	
	//sort year array by year
	sortyearlist = function (year1, year2){
		return (year1 - year2);
	}

//=============== Function to load video list ==========
	createDiv = function(videoid,videotitle,smallinfo,credit,colorcode){
		var result = 0;
		var titlelength =videotitle.length;
		if(titlelength > 20)
			videotitle = videotitle.substring(0, 20)+"..";
			
		var infolength = smallinfo.length;
		if(infolength > 30)
			smallinfo = smallinfo.substring(0, 30)+"..";

		var str_temp = '';
		if(colorcode ==1)
			str_temp = '<div class="dv_scroll" style="text-align:left; background-color:#D3D3D3;">';
		else
			str_temp = '<div class="dv_scroll" style="text-align:left; background-color:#E5E5E5;">';

			str_temp +=	'<li><table width=100%><tr valign="top"><td onclick="showvideodetails('+videoid+');" rowspan="2" width=5% class="videothumb01"><a href="#dv_videodetails"><img src="themes/apple/cpd/video_thumb.png"></a></td><td onclick="showvideodetails('+videoid+');" class="videotitle01" width=60%><a href="#dv_videodetails">'+videotitle+'</a></td><td rowspan="2" align="right" width=2%><img src="themes/apple/cpd/verticle_bar.png"></td></tr><tr><td onclick="showvideodetails('+videoid+');" class="videodesc01" valign=top width=60%><a href="#dv_videodetails">'+smallinfo+'</a></td><td valign="top" align="right" width=40% ><span class="spn05" id="ifpurchased_'+videoid+'">';
			
			result = $.inArray(videoid, vd_subscribed);
			if(result >= 0)
				str_temp +='PURCHASED >';
			else
				str_temp +=credit+' CREDITS >';
				
			str_temp +='</span></td></tr></table></li>';
		return str_temp;
	}
	
	//SHOW VIDEO THUMBNAILS
	showvideothumb = function() {
		var movies = [{"flashvars":{"poster":"http://www.osmf.org/dev/1.6gm/images/poster1.png", 				  
							"src":"http://mediadelivery.adobe.com/adobevideodemo/manifests/hls-hillmancurtis-manifest.m3u8"
							 /*"src":"http://gajanan.srfms.com:2097/vod/mp4:sample.mp4/playlist.m3u8"*/
					 },"element":"dv_videothumb"}]; 
		for (i = 0; i < movies.length; i++) {
		strobeMediaPlayback.flashvars(movies[i]["flashvars"]);
		strobeMediaPlayback.draw(movies[i]["element"]);
		}
	}

	//add video for favourite list
	$("#dv_showvideodetails").delegate("div.click_for_fav", "touchstart", function(){
		var favourite_button = $(this);
		var vid = favourite_button.attr('id');
			$.ajax({
				url: BASEURL+'home/addtofavourite',
				type: "POST",
				dataType: '',
				data: {videoid:vid, uid:userid},
				success: function(responce){
					if(responce == "success"){
						$("div .add_fav_"+vid).animate({ width: 'show', opacity: 'show' , direction: "right" }, 200);
						favourite_button.hide();
						
						vd_favourite.push(vid);
						for(key in vd_all){
							if(vid == vd_all[key]['videoid']){
								var colorcode = vd_favourite.length % 2;
								var newdiv 	= createDiv(vd_all[key]["videoid"],vd_all[key]["videotitle"],vd_all[key]["smallinfo"],vd_all[key]["credit"],colorcode);
								if(vd_favourite.length == 1)
									$("#dv_favouritesvideolist").html(newdiv);
								else
									$("#dv_favouritesvideolist").append(newdiv);
							}
						}
					}
				},
				error: function() {
					showAlert("System Error","There has been an Error with the Settings.");
				}
			});
	});
	
	//remove video from favourite list 
	$("#dv_showvideodetails").delegate("div.click_for_unfav", "touchstart", function(){
		flag_operation  	= $(this);
		flag_confirm_id		=	1;
		showConfirm('Favourite video', 'Do you really want to remove this video from favourite list?', 'Yes,No');
	});

	function remove_favourite(unfavourite_button){
		flag_operation 		=	'';
		flag_confirm_id		=	0;
		//$('.toast-container').fadeOut(300).remove();
		//$('.toast-msgboxOverlay').fadeOut(300);
		var vid = unfavourite_button.attr('id');
			$.ajax({
				url: BASEURL+'home/removefavourite',
				type: "POST",
				dataType: '',
				data: {videoid:vid, uid:userid},
				success: function(responce){
					if(responce == "success"){
						$("div .add_unfav_"+vid).animate({ width: 'show', opacity: 'show' , direction: "right" }, 200);
						unfavourite_button.hide();
						
						vd_favourite = jQuery.grep(vd_favourite, function(value) {
							return value != vid;
						});
						$("#dv_favouritesvideolist").html('');
						showvideolist(vd_favourite,"dv_favouritesvideolist");
					}
				},
				error: function() {
					showAlert("System Error","There has been an Error with the Settings.");
				}
			});
	}
	//======= function to show subscribed, downloaded, favoured, latest video list ======
	showvideolist = function(videolist, dv){ 
	 var count = 1;
	 var colorcode = 0;
		if(videolist.length > 0){
			for(key in vd_all){
			var result = $.inArray(vd_all[key]['videoid'], videolist);
				if(result >= 0){
					colorcode = count%2;
					var newdiv 	= createDiv(vd_all[key]["videoid"],vd_all[key]["videotitle"],vd_all[key]["smallinfo"],vd_all[key]["credit"],colorcode);
					count++;
					$("#"+dv).append(newdiv);
				}
			}
		}else
			$("#"+dv).append(emptyData);// if there are no subscribed videos
		return true;	
	}

//====== function to show year list for all videos ======
	showyearlist = function(yearlist){
		for (var j=0; j< yearlist.length;j++){	
			var counter = 0;
			if(vd_all.length >0){ 
				for(key in vd_all){
					if(yearlist[j] == vd_all[key]["chapteryear"]){
						counter++;
					}
				}//efo  for(key in allvideolist[i])
			}//efo  for key
			var newdiv = '<li class="arrow"><a href="#dv_sortbyYear" onclick="showyearwiselist('+yearlist[j]+');"><span class="spn01" style="color:#90001F;">'+yearlist[j]+'</span> <small class="counter">'+counter+'</small></a></li>';

			$("#allvideo_list").append(newdiv); // add li to all videos listing by year and video count
		}//efo  if yearlist 
		return true;	
	}

	//====== function to show yearwise video list for all videos ======
	showyearwiselist = function(year){
		$("#dv_videosSorted").html('');
		$("#year_head").html('');
		var colorcode = 0;
		var count = 0;
		var newdiv = '';
		for(key in vd_all){
			colorcode = count%2;
			if(year == vd_all[key]["chapteryear"]){
				newdiv = createDiv(vd_all[key]["videoid"],vd_all[key]["videotitle"],vd_all[key]["smallinfo"],vd_all[key]["credit"],colorcode);
				$("#dv_videosSorted").append(newdiv);
				$("#year_head").html(year);
				count++;
			}
		}
	}
//======== function to show details of all videos http://rohitdhore.srfms.com:2221/vod/mp4:'+vd_all[key]["filename"]+'/playlist.m3u8======	
	showvideodetails = function(vid){
		var result	= -1;
		if(vd_all.length >0){ 
			for(key in vd_all){
				if(vid == vd_all[key]["videoid"]){
					details_begins = '<ul class="edgetoedge"><li><table width=100%><tr valign="top"><td class="videotitle02" width=70%><a href="#">'+vd_all[key]["videotitle"]+'</a></td><td rowspan="2" align="right" width="30%" style="margin:0 0 0 10px;" id="current_'+vd_all[key]["videoid"]+'" ><div style="float:right;">';
					
					details_ends ='</div></td></tr><tr><td valign=top class="videodesc02" width=70%><a href="#">'+vd_all[key]["smallinfo"]+'</a></td></tr><tr><td valign=top colspan="3" class="videolongdesc"><div style="float:left; padding:0 10px 0 0; width:100px;"><a href="#"><img src="themes/apple/cpd/video_thumb.png"></a></div>'+vd_all[key]["fulldescription"]+'</td></tr></table></li></ul>';

					result = $.inArray(vd_all[key]['videoid'], vd_favourite); 
					if(result >= 0){
						div_favourite ='<div class="btn blue-favourite click_for_fav add_unfav_'+ vd_all[key]["videoid"]+'" style="display:none;" id="'+ vd_all[key]["videoid"]+'"><a href="javascript:void(0);"><span>Add to Favourite</span></a></div><div class="btn blue-favourite click_for_unfav add_fav_'+ vd_all[key]["videoid"]+'" id="'+ vd_all[key]["videoid"]+'"><a href="javascript:void(0);"><span>Remove Favourite</span></a></div>';
					}else{
						div_favourite ='<div class="btn blue-favourite click_for_fav add_unfav_'+ vd_all[key]["videoid"]+'" id="'+ vd_all[key]["videoid"]+'"><a href="javascript:void(0);"><span>Add to Favourite</span></a></div><div class="btn blue-favourite click_for_unfav add_fav_'+ vd_all[key]["videoid"]+'" style="display:none;" id="'+ vd_all[key]["videoid"]+'"><a href="javascript:void(0);"><span>Remove Favourite</span></a></div>';
					}
					
					result = $.inArray(vd_all[key]['videoid'], vd_subscribed); 
					if(result >= 0){
						div_purchased =	'<div class="btn green-btn"><a href="#"><span>Download</span></a></div>';
						details_ends ='</div></td></tr><tr><td valign=top class="videodesc02" width=70%><a href="#">'+vd_all[key]["smallinfo"]+'</a></td></tr><tr><td valign=top colspan="3" class="videolongdesc"><div style="float:left; padding:0 10px 0 0; width:100px;"><a href="#"><video width="100" height="80" poster="http://lawvideoapp.codezone.in/poster/video_thumb.png" preload="none" tabindex="0"><source src="http://mediadelivery.adobe.com/adobevideodemo/manifests/hls-hillmancurtis-manifest.m3u8"></source></video></a></div>'+vd_all[key]["fulldescription"]+'</td></tr></table></li></ul>';
					}else{
						div_purchased 	= '<div class="btn green-btn buy_btn_'+vd_all[key]["videoid"]+'"><a href="#"><span id ="'+vd_all[key]["videoid"]+'" class="v3btn_purchased" >Buy</span></a></div><div style="display:none" class="btn yellow-btn purchase_btn_'+vd_all[key]["videoid"]+'"><a href="#"><span id ="'+vd_all[key]["videoid"]+'" class="v3_buythisvideo" >'+vd_all[key]["credit"]+' Credit</span></a></div>';
					}
					
					var details = details_begins+div_favourite+"</br>"+div_purchased+"</br>"+details_ends;
					$("#dv_showvideodetails").html(details);
				}
			}
		}
	}
	
	//after clicking purchase video button
	$("#dv_showvideodetails").delegate("span.v3btn_purchased", "touchstart", function(){
		var buy_button = $(this);
		var videoid = buy_button.attr('id');
		$("div .buy_btn_"+videoid).hide();
		$("div .purchase_btn_"+videoid).animate({ width: 'show', opacity: 'show' , direction: "right" }, 200);
		flag_buy_videoid 		= videoid;
		flag_buy_status 		= 1;
	});
	
	//reset buy button
	get_buybtn = function(){
		var videoid = flag_buy_videoid;
		$("div .purchase_btn_"+videoid).hide();
		$("div .buy_btn_"+videoid).animate({ width: 'show', opacity: 'show' , direction: "right" }, 200);
		flag_buy_videoid 		= 0;
		flag_buy_status 		= 0;
	}
	
	//confirm purchasing
	$("#dv_showvideodetails").delegate("span.v3_buythisvideo", "tap", function(){
		flag_operation  	= $(this);
		flag_confirm_id		=	2;
		showConfirm('Download video', 'Do you really want to purchase this video?', 'Yes,No');
	});//eof purchase video
	
	//purchase after confirmation
	function final_purchase(purchase_button){
		flag_operation ='';
		flag_confirm_id		=	0;
		//$('.toast-container').fadeOut(300).remove();
		//$('.toast-msgboxOverlay').fadeOut(300);
		$.ajax({
			url			: BASEURL+'home/subscribevideo',
			type		: "POST",
			dataType: '',
			data		: {vdoid:purchasethisvideo, userid:userid},
			success		: function(responce){
				if(responce == "error1"){
					showAlert("Purchase Video","Your credit balance is low to buy this video.");
				}else
				if(responce == "error2"){
					showAlert("System Error","Technical error please try later.");
				}
				else{
					flag_buy_status = 0;
					flag_buy_videoid = 0;
					showAlert("Purchase Video","Video purchased successfully.");                        
								 
					$(".purchase_btn_"+purchasethisvideo).removeClass("yellow-btn").addClass("green-btn").html('<a href="#"><span class="v3btn_download" >Download</span></a>');
					vd_subscribed.push(purchasethisvideo);
					for(key in vd_all){
						if(vd_all[key]["videoid"]==purchasethisvideo){
							var colorcode = vd_subscribed.length %2;
							var newdiv 	= createDiv(vd_all[key]["videoid"],vd_all[key]["videotitle"],vd_all[key]["smallinfo"],vd_all[key]["credit"],colorcode);
							if(vd_subscribed.length == 1)
								$("#myvideolist").html(newdiv);
							else
								$("#myvideolist").append(newdiv);
							$("span#ifpurchased_"+purchasethisvideo).html(' PURCHASED >');
							$("#spn_totalvdpurchased").html(" ( "+vd_subscribed.length+" Videos)");
							$(".subscribe_count").html(vd_subscribed.length);
						}
					}
				}
			},//eof ajax success
			error: function() {
			showAlert("System Error","There has been an Error with the Settings");
			}
		});
	}
	//confirm logout
	confirm_logout = function(){
	//$('.toast-container').fadeIn(300);
		flag_confirm_id		=	3;
		showConfirm('Log Out', 'Do you really want to logout?', 'Yes, No');
		return;
	}
	
	//logout after confirmation
	function logout(){
		flag_operation ='';
		flag_confirm_id		=	0;
		var url = "index.html";
		$(location).attr("href",url);
	}
	
});

	//function to get all video data on app load
	getalldata = function(userid){
		$.ajax({
			url				: BASEURL+'home/getdata',
			type			: "POST",
			dataType	: 'json',
			data			: {userid:userid},
			success		: function(responce){
				$("#txt_loginid").val('');
				$("#txt_password").val('');
				if(responce.status == 'error'){
					showAlert("Log In","Incorrect Login Details.");
				}else{
					if(responce){
						$("#spn_all").html("("+responce.allvideoscount+")");
						$("#spn_latest").html("("+responce.latesttvideoscount+")");

						var flag 	= 0;
						var cnt 	= 2;
						var index =	0;
						var allvideolist 		= new Array(responce.avlist);
						var latestvideolist = new Array(responce.ltlist);
						userdetails 				= responce.userdetails;
						subscribevideolist 	= responce.sublist;
						//add credits list to buy credit
						var creditlimit 		= "";
						var creditlimitlist 			= new Array(responce.creditlist);
						for (var i =0; i< creditlimitlist.length;i++){
							for(var j=0; j< creditlimitlist[i].length;j++){
							creditlimit +='<li class="arrow_buy"><span class="spn06">'+creditlimitlist[i][j]["credit"]+' Credits</span><span class="spn03"> ($ '+creditlimitlist[i][j]["creditprice"]+')</span><span class="spn07">BUY</span></li>';
							}
						}//eof credit list
						$("#dv_buycredit_list").html(creditlimit);
						if(responce.subvideoscount > 0){
							for (var i =0; i< subscribevideolist.length;i++){
								if(subscribevideolist[i]['subscribe'] == 1)
									vd_subscribed.push(subscribevideolist[i]['videoid']);	//subscribed videos(id) array
								if(subscribevideolist[i]['download'] == 1)
									vd_downloaded.push(subscribevideolist[i]['videoid']);	//downloaded videos(id) array
								if(subscribevideolist[i]['favourite'] == 1)
									vd_favourite.push(subscribevideolist[i]['videoid']);	//favourite videos(id) array
							}
						}

						//feel credit details on my account tab====
						for (var i =0; i< userdetails.length;i++){
							for(var j=0; j< userdetails[i].length;j++){
								$("#spn_creditremaining").html(" ( "+userdetails[i][j]['credit']+" Credits remaining)");	
							}
						}
						$("#spn_totalvdpurchased").html(" ( "+vd_subscribed.length+" Videos)");	
						$("#spn_totalcdtpurchased").html(" ( "+responce.totalcreditpurchased+" )");	
						//end of credit details=====================

						if(responce.latesttvideoscount > 0){
							for (var i =0; i< latestvideolist.length;i++){
								for(var j=0; j< latestvideolist[i].length;j++){
									vd_latest.push(latestvideolist[i][j]['videoid']);						//latest videos array
								}
							}
						}
						if(responce.allvideoscount > 0){
							for (var i =0; i< allvideolist.length;i++){
								for(var j=0; j<allvideolist[i].length;j++){
									var flag1 = (jQuery.inArray(allvideolist[i][j]["chapteryear"], yearlist));
									vd_all.push(allvideolist[i][j]);							//all videos array
									if(flag1 == -1){
										yearlist[index] = allvideolist[i][j]["chapteryear"]; //array of years
										index++;
									}
								}
							}
						}

						yearlist.sort(sortyearlist);//sorted year list

						var dv_homelisting = '<ul id="thelist" class="edgetoedge"><li class="arrow"><a href="#myvideos"><span class="spn01" style="color:#90001F;">My Videos</span> <small class="counter subscribe_count">'+vd_subscribed.length+'</small></a></li><li class="arrow"><a href="#latestvideos"><span class="spn01" style="color:#90001F;"> Latest Videos</span> <small class="counter">'+responce.latesttvideoscount+'</small></a></li><li class="arrow"><a href="#allvideos"><span class="spn01" style="color:#90001F;"> All Videos</span> <small class="counter" >'+responce.allvideoscount+'</small></a></li></ul>';

						$("#dv_homelisting").append(dv_homelisting); // append listing to home page

						//function calls to load various listings
						flag = showvideolist(vd_subscribed,"myvideolist"); 
						flag = showvideolist(vd_favourite,"dv_favouritesvideolist");
						flag = showvideolist(vd_downloaded,"dv_dwnldvideolist");
						flag = showvideolist(vd_latest,"dv_latestvideolist");	
						flag = showyearlist(yearlist);
						managevideolist();
					}//eof if repsonce
				}
			},//eof ajax success
			error: function() {
				showAlert("System Error","There has been an Error with the Settings.");
			}
		});
	}//eof getalldata

managevideolist = function(){    
	var result_dwn = -1;
	var result_fav = -1;
	var dw_vd_list = '';

	var check				= 0;
	$("#dv_managevideoslist").html('');
	for (var j=0; j< yearlist.length;j++){
		check  = 0;
        dw_vd_list += '<li>';

		for(key in vd_all){
			result_dwn = -1;
			result_fav = -1;

			if(yearlist[j] == vd_all[key]['chapteryear']){
				result_dwn = $.inArray(vd_all[key]['videoid'], vd_downloaded);
				result_fav = $.inArray(vd_all[key]['videoid'], vd_favourite);

				if(result_dwn >= 0 || result_fav >= 0){
					if(check == 0){
						dw_vd_list += '<div style="font-weight:bold; height:24px; margin: 0px 8px;">'+yearlist[j]+'</div>';
						check =1;
					}
				}
				if(result_dwn >= 0){
					dw_vd_list += '<div style="padding:2px; border:1px solid grey; height:22px; font-size:14px; background-color:white; margin: 5px 8px;"><input type="checkbox" name="chk_dwnload[]" class="checkbox" id="chk_dwnload_'+vd_all[key]["videoid"]+'" value="'+vd_all[key]["videoid"]+'"/> '+ vd_all[key]["videotitle"]+'<span class="dwn_image"></span></div>';
				}
				if(result_fav >= 0){
					dw_vd_list += '<div style="padding:2px; border:1px solid grey; height:22px; font-size:14px; background-color:white; margin: 5px 8px;"><input type="checkbox" class="checkbox" name="chk_favourite[]" id="chk_favourite_'+vd_all[key]["videoid"]+'" value="'+vd_all[key]["videoid"]+'"/> '+ vd_all[key]["videotitle"]+'<span class="fav_image"></span></div>';
				}
			}
		}
       dw_vd_list += '</li>';
	}
    $("#dv_managevideoslist").append(dw_vd_list); 
    //change height on orientation detection
    
    //jQT.setPageHeight(); 
}
//Set param as per orientation
doOnOrientationChange();